/*
  Programa GEFE - Gestión de eventos lúdicos.
  Copyright (C) 2010  Antonio Ramírez Marti

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "nuevoasistentedialog.h"
#include "ui_nuevoasistentedialog.h"

#include <QList>
#include <QStandardItem>

NuevoAsistenteDialog::NuevoAsistenteDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NuevoAsistenteDialog)
{
    ui->setupUi(this);
    setup();
}

NuevoAsistenteDialog::~NuevoAsistenteDialog()
{
    delete ui;
}

bool NuevoAsistenteDialog::getPagoPorDefecto() const
{
    return this->pagoPorDefecto;
}

void NuevoAsistenteDialog::setPagoPorDefecto(bool estado)
{
    this->pagoPorDefecto=estado;
}

void NuevoAsistenteDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void NuevoAsistenteDialog::setup()
{
    this->modeloAsistentes=new QStandardItemModel(this->parent());
    this->mapper=new QDataWidgetMapper(this);
    setPagoPorDefecto(true);

    mapper->setModel(modeloAsistentes);

    mapper->addMapping(ui->nombreEdit,0);   ///< Nombre
    mapper->addMapping(ui->apellidosEdit,1);
    mapper->addMapping(ui->dniEdit,2);
    mapper->addMapping(ui->emailEdit,3);
    mapper->addMapping(ui->ingresoCheckBox,4);

    mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);

    ui->vistaAsistentes->setModel(modeloAsistentes);
    ui->vistaAsistentes->verticalHeader()->setVisible(false);
    ui->vistaAsistentes->horizontalHeader()->setVisible(false);

    connect(ui->nuevoAsistenteButton,SIGNAL(clicked()),this,SLOT(addAsistente()));
    connect(ui->vistaAsistentes->selectionModel(),SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),mapper,SLOT(setCurrentModelIndex(QModelIndex)));
    connect(ui->buttonBox,SIGNAL(accepted()),this,SLOT(sendAsistentes()));
    connect(ui->pagoDefectoCheckBox,SIGNAL(toggled(bool)),this,SLOT(setPagoPorDefecto(bool)));

    mapper->toFirst();

    addAsistente();
    ui->vistaAsistentes->setCurrentIndex(modeloAsistentes->index(0,0));
}

void NuevoAsistenteDialog::addAsistente()
{
    QList<QStandardItem*> asistente;
    QStandardItem *pago;

    for(int i=0;i<4;i++)
        asistente.append(new QStandardItem());

    pago=new QStandardItem();
    pago->setData(QVariant(this->getPagoPorDefecto()),Qt::EditRole);
    asistente.append(pago);
    modeloAsistentes->appendRow(asistente);
}

void NuevoAsistenteDialog::sendAsistentes()
{
    emit this->nuevosAsistentes(modeloAsistentes);
}
